package com.regis.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regis.comment.CodeMsg;
import com.regis.entity.db.Employee;
import com.regis.entity.ro.EmployeeAddRo;
import com.regis.entity.ro.LoginRo;
import com.regis.entity.vo.ResultVo;
import com.regis.comment.GlobalException;
import com.regis.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 员工界面接口
 * @date 2022/7/28 4:46
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 员工登录
     * @param loginRo
     * @return
     */
    @PostMapping("/login")
    public ResultVo<Employee> login(HttpServletRequest request, @RequestBody LoginRo loginRo){
        //密码加密
        String password = SaSecureUtil.md5(loginRo.getPassword());

        //查询用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,loginRo.getUsername());
        Employee employee = employeeService.getOne(queryWrapper);

        if(Objects.isNull(employee)){
            throw  new GlobalException(new CodeMsg("用户不存在"));
        }

        if(!employee.getPassword().equals(password)){
            throw new GlobalException(new CodeMsg("密码错误"));
        }

        if(employee.getStatus() == 0){
            throw new GlobalException(new CodeMsg("账号已经禁用"));
        }

        request.getSession().setAttribute("employee",employee.getId());
//        redisTemplate.opsForValue().set(request.getSession().getId(),employee.getId());

        return ResultVo.success(employee);
    }

    /**
     * 员工登出
     * @return
     */
    @PostMapping("/logout")
    public ResultVo<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return ResultVo.success("登出成功");
    }

    /**
     * 添加员工
     */
    @CacheEvict(value = "employee",allEntries = true)
    @PostMapping
    public ResultVo<String> save(HttpServletRequest request,@RequestBody EmployeeAddRo employeeAddRo){
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeAddRo,employee);

        //设置初始密码
        employee.setPassword(SaSecureUtil.md5("123456"));

        employeeService.save(employee);

        return ResultVo.success("添加成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Cacheable(value = "employee",key = "#page + '_' + #pageSize",condition = "#name == null ",
            unless = "#result.data.records == null")
    @GetMapping("/page")
    public ResultVo<Page<Employee>> page(int page,int pageSize,String name){
        //分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //过滤条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);
        return ResultVo.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @CacheEvict(value = "employee",allEntries = true)
    @PutMapping
    public ResultVo<String> update(HttpServletRequest request,@RequestBody Employee employee){
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return ResultVo.success("员工信息修改成功");
    }

    /**
     * 根据id获取一个用户的信息
     * @param id
     * @return
     */
    @Cacheable(value = "employee",key = "#id",unless = "#result.data == null")
    @GetMapping("/{id}")
    public ResultVo<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(!Objects.isNull(employee)){
            return ResultVo.success(employee);
        }
        return ResultVo.error("用户不存在");
    }
}
